package com.atguigu.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.constant.MessageConstant;
import com.atguigu.constant.RedisMessageConstant;
import com.atguigu.entity.Result;
import com.atguigu.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    JedisPool jedisPool;
    @Reference
    OrderService orderService;

    /*
    * map 代表前端表单
    * */
    @RequestMapping("/submit.do")
    public Result order(@RequestBody Map map){
        //获取手机号
        String telephone = map.get("telephone").toString();
        //获取客户输入的验证码
        String validateCode = map.get("validateCode").toString();
        //获取redis输入的验证码
        String redisCode = jedisPool.getResource().get(telephone+ RedisMessageConstant.SENDTYPE_ORDER);
        //比较验证码
        if (redisCode == null || !validateCode.equals(redisCode)){
            return  new Result(false, "验证失败");
        }
        //下订单  把数据保存(添加)到数据库
        Result result = null;
        try {
          result = orderService.addOrder(map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDER_FAIL);
        }
        return  result;
    }

    @RequestMapping("/findById.do")
    public Result findById(Integer id) throws Exception {
        Map map =  orderService.findById(id);
        return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
    }
}
